package com.dhcc.bpm.modules.scf.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.scf.dto.ScfSubjectBasicInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfSubjectBasicInfo;
import com.dhcc.bpm.modules.scf.service.IScfSubjectBasicInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfSubjectBasicInfoVO.ScfGetSubjectDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfSubjectBasicInfoVO.ScfGetSubjectListVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.dhcc.bpm.common.utils.StringUtils;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 主体基本信息控制器
 * liuguoting
 * 2023-09-06
 */
@RestController
@AllArgsConstructor
@Slf4j
@Validated
public class ScfSubjectBasicInfoController extends BladeController {

    private IScfSubjectBasicInfoService scfSubjectBasicInfoService;


    /**
     * 查询主体分页列表
     *
     * @param scfSubjectBasicInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetSubjectList")
    // @Cacheable(value = "scfSubjectListCache", key = "'scfSubjectListCacheKey-' + #scfSubjectBasicInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetSubjectListVO>> page( ScfSubjectBasicInfoDTO scfSubjectBasicInfo, Query query) {
        if (StringUtils.isEmpty( scfSubjectBasicInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }

        scfSubjectBasicInfo.setOrgCodes(  scfSubjectBasicInfo.getOrgCode().split(",") );
        if(scfSubjectBasicInfo.getOrgCode().contains( "50000000")){
            scfSubjectBasicInfo.setOrgCode("50000000");
            scfSubjectBasicInfo.setOrgCodes("50000000".split(",") );
        }
        return R.data(scfSubjectBasicInfoService.selectScfSubjectBasicInfoPage(Condition.getPage(query), scfSubjectBasicInfo));
    }
    /**
     * 查询主体详细信息
     *
     * @param scfSubjectBasicInfo
     * @return
     */
    @GetMapping("/scfGetSubjectDetails")
    // @Cacheable(value = "scfSubjectDetailsCache", key = "'scfSubjectDetailsCacheKey-' + #scfSubjectBasicInfo.rowId")
    public R<ScfGetSubjectDetailsVO> details(ScfSubjectBasicInfoDTO scfSubjectBasicInfo) {
        log.info("接收到详情查询参数：{}", scfSubjectBasicInfo);
        return R.data(scfSubjectBasicInfoService.getDetails(scfSubjectBasicInfo));
    }

    /**
     * 新增主体信息
     *
     * @param scfSubjectBasicInfo
     * @return
     */
    @PostMapping("/scfAddSubjectDetails")
    // @CacheEvict(value = "scfSubjectListCache", allEntries = true)
    public R<Boolean> saveSubjectBasic(@RequestBody ScfSubjectBasicInfoDTO scfSubjectBasicInfo) throws Exception {
        return R.status(scfSubjectBasicInfoService.saveSubjectBasic(scfSubjectBasicInfo));
    }

    /**
     * 修改主体信息
     *
     * @param scfSubjectBasicInfo
     * @return
     */
    @PostMapping("/scfModSubjectDetails")
    // @CacheEvict(value = "scfSubjectDetailsCache", key = "'scfSubjectDetailsCacheKey-' + #scfSubjectBasicInfo.rowId")
    public R<Boolean> updateSubjectBasic(@RequestBody ScfSubjectBasicInfoDTO scfSubjectBasicInfo) {
        return R.status(scfSubjectBasicInfoService.updateSubjectBasic(scfSubjectBasicInfo));
    }

    /**
     * 批量导入主体信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfSubjectBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfSubjectBasicInfo> scfSubjectBasicInfos = scfSubjectBasicInfoService.batchSaveSubjectBasic(operatorId, file);
        int bachSaveSubjectBasic = scfSubjectBasicInfoService.bachSaveSubjectBasic(scfSubjectBasicInfos, operatorId);
        int total = scfSubjectBasicInfos.size();
        int totalSussess = bachSaveSubjectBasic;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }


    /**
     * 删除
     */
    @PostMapping("/scfGetSubject/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        String[] split = ids.split(",");
        return R.status(scfSubjectBasicInfoService.deleteByIds(split ));
    }

}
